// renwu/jianchajieguo/jianchajieguo.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modal: true, //模态框
    dex: 0, //结果中单位的index
    numszhi: "", //输入的数值
    resultindex: null, //结果 index
    xuanzekuang: false, //结果中单位的模态框
    selresult: [],
    result: []
  },
  disscroll() {},
  // 关闭模态框
  closemodal() {
    this.setData({
      modal: true
    })
  },
  // 显示模态框
  inputdanwei(e) {
    console.log(e.currentTarget.dataset.index);
    this.setData({
      resultindex: e.currentTarget.dataset.index,
      modal: false
    })
  },
  // 重置表单
  resetform() {
    this.setData({
      numszhi: "",
      modal: true
    })
  },
  // 提交表单
  formsubmit(e) {
    var that = this;
    var arr = that.data.result;
    var dex = that.data.resultindex;
    var val = e.detail.value;
    var erjid = that.data.erjid
    if (val.nums == "") {
      wx.showToast({
        title: '请输入数值',
      })
    } else {
      arr[dex].dwnum = val.nums + val.jc_danwei;
      that.setData({
        numszhi: "",
        modal: true,
        result: arr
      })

    }
  },
  // 单位选择框显示
  danweisel() {
    this.setData({
      xuanzekuang: true
    })
  },
  submit: function() {
    //保存数据va
    var that = this
    var xm_id = that.data.xm_id
    var val = that.data.result
    var erjid = that.data.erjid
    console.log(that.data.selresult)
    if (that.data.selresult.length !== 0) {
      if (that.data.jcx_id !== undefined) {
        console.log(that.data.jcx_id)
        //更新
        app.util.request({
          url: 'entry/wxapp/Savejcjg',
          data: {
            val: that.data.selresult,
            erjid: erjid,
            jcx_id2: that.data.jcx_id,
            jxopenid: wx.getStorageSync('openid'),
            duox: 1,
            xm_id: xm_id
          },
          success: function(res) {
            console.log(res)
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
              jcx_id: res.data.data
            })
            wx.navigateBack({
              delta: 1
            })
          }
        })
      } else {
        app.util.request({
          url: 'entry/wxapp/Savejcjg',
          data: {
            val: that.data.selresult,
            erjid: erjid,
            jxopenid: wx.getStorageSync('openid'),
            duox: 1,
            xm_id: xm_id
          },
          success: function(res) {
            console.log(res)
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
              jcx_id: res.data.data
            })
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }

    } else {
      if (that.data.jcx_id !== undefined) {
        console.log(that.data.jcx_id)
        //更新
        app.util.request({
          url: 'entry/wxapp/Savejcjg',
          data: {
            val: that.data.result,
            erjid: erjid,
            jcx_id2: that.data.jcx_id,
            jxopenid: wx.getStorageSync('openid'),
            xm_id: xm_id
          },
          success: function(res) {
            console.log(res)
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
              jcx_id: res.data.data
            })
            wx.navigateBack({
              delta: 1
            })
          }
        })
      } else {
        app.util.request({
          url: 'entry/wxapp/Savejcjg',
          data: {
            val: that.data.result,
            erjid: erjid,
            jxopenid: wx.getStorageSync('openid'),
            xm_id: xm_id
          },
          success: function(res) {
            console.log(res)
            var pages = getCurrentPages();
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
              jcx_id: res.data.data
            })
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    }
  },
  //选择单位
  quedingdw(e) {
    this.setData({
      dex: e.currentTarget.dataset.index,
      xuanzekuang: false
    })
  },

  //多选
  selmul(e) {
    var that = this;
    var dex = e.currentTarget.dataset.index;
    var arr = that.data.selresult;
    arr[dex].seltrue = !arr[dex].seltrue;
    console.log(arr);
    that.setData({
      selresult: arr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var xm_id = options.xm_id
    var erjid = options.erjid
    var multtypes = options.multtypes
    var bgc = wx.getStorageSync('color')
    var multsel = options.multsel
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    if (options.jcx_id !== undefined) {
      // 查询记录
      app.util.request({
        url: 'entry/wxapp/Getdatajcjg',
        data: {
          jcx_id: options.jcx_id
        },
        success: function(res) {
          console.log(res)
          if (res.data.data.duox == 1) {
            that.setData({
              selresult: res.data.data.contents
            })
          }
          that.setData({
            bgc: bgc,
            result: res.data.data.contents,
            multsel: multsel,
            jcx_id: options.jcx_id,
          })
        }
      })
      that.setData({
        jcx_id: options.jcx_id
      })
    } else {
      app.util.request({
        url: 'entry/wxapp/Getjcjg',
        data: {
          xm_id: xm_id
        },
        success: function(res) {
          console.log(res)
          var arrs = res.data.data;
          if (options.multsel == 1) {
            for (var i = 0; i < arrs.length; i++) {
              arrs[i].dwnum = "";
            }
            that.setData({
              result: arrs
            })
          } else if (options.multsel == 0) {
            for (var i = 0; i < arrs.length; i++) {
              arrs[i].seltrue = false;
            }
            that.setData({
              selresult: arrs
            })
          }
          that.setData({
            bgc: bgc,
            multsel: options.multsel,
            erjid: erjid
          })
          console.log(options.multsel)
        }
      })
    }
    that.setData({
      bgc: bgc,
      multsel: options.multsel,
      erjid: erjid,
      multtypes: multtypes,
      xm_id: xm_id
    })
  },
  submitbtns(e) {
    var that = this;
    var val = e.detail.value;
    var erjid = that.data.erjid
    if (that.data.jcx_id !== undefined) {
      console.log(that.data.jcx_id)
    } else {
      app.util.request({
        url: 'entry/wxapp/Savejcjg',
        data: {
          val: val,
          erjid: erjid,
          jxopenid: wx.getStorageSync('openid')
        },
        success: function(res) {
          console.log(res)
        }
      })
    }
    if (that.data.multsel == 0) {
      var arr = that.data.selresult;
    } else if (that.data.multsel == 1) {
      var arr = that.data.result;
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})